package cn.jbooter.repository.autoconfigure.second;

import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import cn.jbooter.repository.autoconfigure.properties.RepositoryProperties;

@Configuration
@ConditionalOnClass({ SqlSessionFactory.class, SqlSessionFactoryBean.class })
@ConditionalOnBean(name="secondDataSource")
@AutoConfigureAfter(SecondDataSourceConfigure.class)
@ConditionalOnProperty(prefix="spring.datasource.druid.second",name="enabled",havingValue="true")
@EnableConfigurationProperties({
	RepositoryProperties.class
})
public class SecondMybatisConfigure {
	private static final Logger logger = LoggerFactory.getLogger(SecondMybatisConfigure.class);
	
	@Autowired
	private RepositoryProperties repositoryProperties;
	
	/**
	 * 主数据源mybatis的sqlSessionFactory配置
	 * 在mybatis的配置文件中配置分页拦截器
	 * 
	 * @return
	 * @throws Exception
	 */
    @Bean(name="secondSqlSessionFactory")
	public SqlSessionFactory secondSqlSessionFactory(@Qualifier("secondDataSource") DataSource secondDataSource){
    	logger.info("===>>>初始化第2数据源mybatis的SqlSessionFactory......");
    	SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
    	sqlSessionFactoryBean.setDataSource(secondDataSource);
    	ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    	sqlSessionFactoryBean.setConfigLocation(resolver.getResource(repositoryProperties.getSecond().getMybatisConfigLocation().trim()));
    	try {
    		sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath*:/**/*-mapper.xml"));
    		SqlSessionFactory object = sqlSessionFactoryBean.getObject();
    		logger.info("===>>>初始化第2数据源mybatis的SqlSessionFactory完成.");
			return object;
		} catch (Exception e) {
			logger.error("===>>>初始化第2数据源mybatis的sqlSessionFactory失败:"+e.getMessage());
			e.printStackTrace();
			throw new RuntimeException(e);
		}
    }
    
    /**
     * 第2数据源的SqlSessionTemplate配置
     * @return
     */
	@Bean
	public SqlSessionTemplate secondSqlSessionTemplate(
			@Qualifier("secondSqlSessionFactory") SqlSessionFactory secondSqlSessionFactory) {
		return new SqlSessionTemplate(secondSqlSessionFactory);
	}
	
	
}
